﻿package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.CardInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.kuanter.kuanterauto.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;

public class ActivateCardNextActivity extends WuyangBaseActivity implements OnClickListener{
	private static final int CHOOSE_CAR_NUMBER = 265;
	private static final int CHOOSE_CAR_TYPE = 267;
	private Intent intent = new Intent();
	
	private String cardNumber;
	
	private String mobileNumber;
	private DataLoader dataLoader;
	
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status
							com.alibaba.fastjson.JSONObject jsonObjectResult1 = JSON.parseObject(result);
							CardInfo cardInfo = jsonObjectResult1.getObject("cardInfo", CardInfo.class);
							Intent intent = new Intent(ActivateCardNextActivity.this,ActivateCardSuccessedActivity.class);
							if(null != cardInfo.getCardNumber()&& !"".equals(cardInfo.getCardNumber())){
								intent.putExtra("cardNumber", cardInfo.getCardNumber());
							}
							if(null != cardInfo.getPointBalance()&& !"".equals(cardInfo.getPointBalance())){
								intent.putExtra("pointBalance", cardInfo.getPointBalance());
							}else {
								intent.putExtra("pointBalance","暂无");
							}
							if(null != cardInfo.getExpireTime()&& !"".equals(cardInfo.getExpireTime())){
								intent.putExtra("expireTime", cardInfo.getExpireTime());
							}else {
								intent.putExtra("expireTime", "暂无");
							}
							startActivity(intent);
							finishProgress();
						} else {
							finishProgress();
							// 显示返回数据中 errorMsg 字段值
							showToast("失败");
						}
					} catch (JSONException e) {
						finishProgress();
						e.printStackTrace();
					}
				} else {
					finishProgress();
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};
	private View choose_car_number;
	private View choose_car_type;
	private TextView license_plate_nubmer_tv;
	private TextView car_model_tv;
	private EditText user_name_et;
	private String user_name;
	private EditText email_et;
	private String email;
	private String pp;
	private String xh;
	private String license_plate_nubmer;
	private EditText user_password_et;
	private EditText user_password_confirm_et;
	private String user_password;
	private String user_password_confirm;
	private ArrayList<NameValuePair> params;
	
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activate_card_next);
		super.onCreate(savedInstanceState);
		this.setHeaderControlViewText();
		this.initBodyControlView();
		this.setBodyControlViewText();
		this.registerListener();
		//获取上个界面传来的intent
		Intent getIntent = getIntent();
		cardNumber = getIntent.getStringExtra("cardNumber");
		mobileNumber = getIntent.getStringExtra("mobileNumber");
		dataLoader = new DataLoader(this);
		ProApplication.getInstance().getActivityList().add(this);
	}
	@Override
	protected void onDestroy() {
		 ProApplication.getInstance().getActivityList().remove(this);
		super.onDestroy();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			break;
		case R.id.header_right:
			//激活卡片
			this.getControlViewText();
				//封装此处数据
				if(null == user_name||"".equals(user_name)){
					showToast("您没填新用户姓名哦");
				}else if(isE(user_name)){
					showToast("用户姓名要填中文哦");
				}else if(null == user_password|| "".equals(user_password)){
					showToast("您没填新用户密码哦");
				}else if(user_password.length() != 6){
					showToast("密码长度要为六位哦");
				}else if(null == user_password_confirm || "".equals(user_password_confirm)){
					showToast("您还没确认密码哦");
				}else if(user_password_confirm.length() != 6){
					showToast("密码长度要为六位哦");
				}else if(!user_password.equals(user_password_confirm)){
					showToast("两次填写的新用户密码不一样哦");
				}else if(null == email || "".equals(email)){
					showToast("您没填邮箱号哦");
				}else if(false == StringUtils.isEmail(email)){
					showToast("您输入的不是邮箱号哦");
				}else if(null == license_plate_nubmer || "".equals(license_plate_nubmer)){
					showToast("您没选车牌号哦");
				}else if(null == pp || "".equals(pp)){
					showToast("您没选车型哦");
				}else if(null == xh || "".equals(xh)){
					showToast("您没选型号哦");
				}else {
					//上传该数据给服务器激活
					params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("cardNumber",cardNumber));
					params.add(new BasicNameValuePair("mobileNumber",mobileNumber));
					params.add(new BasicNameValuePair("userRealName",user_name));
					params.add(new BasicNameValuePair("userPassword",user_password));
					params.add(new BasicNameValuePair("userEmail",email));
					params.add(new BasicNameValuePair("licensePlateNumber",license_plate_nubmer));
					params.add(new BasicNameValuePair("vehicleBrand",pp));
					params.add(new BasicNameValuePair("vehicleModels",xh));
					dataLoader.DisplayData(MainConstants.HOST_test+HttpConfigAPI.ACTIVATE_CARD, params, handler, "",false) ;
					showProgress("正在加载数据...");
				}
				break;
		case R.id.choose_car_number:
			intent.setClass(this, LincensePlateNumberActivity.class);
			startActivityForResult(intent, CHOOSE_CAR_NUMBER);
			break;
		case R.id.choose_car_type:
			intent.setClass(this, XgcxActivity.class);
			startActivityForResult(intent, CHOOSE_CAR_TYPE);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CHOOSE_CAR_NUMBER:
			if(null != data){
				String provice = data.getStringExtra("province");
				String car_number = data.getStringExtra("car_number");
				license_plate_nubmer = provice+"  "+car_number;
				license_plate_nubmer_tv.setText(license_plate_nubmer);
			}
			break;
		case CHOOSE_CAR_TYPE:
			if(null != data){
				pp = data.getStringExtra("pp");
				xh = data.getStringExtra("xh");
				car_model_tv.setText(pp+"  "+xh);
			}
			break;
		default:
			break;
		}
	}
	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		super.setHeaderTitleText(headerTitle, "卡片激活");
		super.setHeaderLeftText(headerLeft, "返回");
		super.setHeaderRightText(headerRight, "激活");
	}
	@Override
	protected void initBodyControlView() {
		choose_car_number = (View)this.findViewById(R.id.choose_car_number);
		choose_car_type =  (View)this.findViewById(R.id.choose_car_type);
		license_plate_nubmer_tv = (TextView)this.findViewById(R.id.license_plate_nubmer_tv);
		car_model_tv = (TextView)this.findViewById(R.id.car_model_tv);
		user_name_et = (EditText)this.findViewById(R.id.user_name_et);
		email_et = (EditText)this.findViewById(R.id.email_et);
		user_password_et = (EditText)this.findViewById(R.id.user_password_et);
		//设置数字密码输入
		user_password_et.setTransformationMethod(PasswordTransformationMethod.getInstance());
		user_password_et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
		user_password_et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		
		user_password_confirm_et = (EditText)this.findViewById(R.id.user_password_confirm_et);
		user_password_confirm_et.setTransformationMethod(PasswordTransformationMethod.getInstance());
		user_password_confirm_et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
		user_password_confirm_et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
	}
	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		//btn rigester listener
		super.headerLeft.setOnClickListener(this);
		super.headerRight.setOnClickListener(this);
		choose_car_number.setOnClickListener(this);
		choose_car_type.setOnClickListener(this);
	}
	@Override
	protected void getControlViewText() {
		user_name = user_name_et.getText().toString();
		email = email_et.getText().toString();
		user_password = user_password_et.getText().toString();
		user_password_confirm = user_password_confirm_et.getText().toString();
	}

}	
